<% import grails.persistence.Event %>
<%=packageName%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <g:set var="entityName" value="\${message(code: '${domainClass.propertyName}.label', default: '${className}')}" />
        <title><g:message code="default.edit.label" args="[entityName]" /></title>
    </head>
    <body>
        <div class="nav">
            <span class="menuButton"><a class="home" href="\${createLink(uri: '/')}">Home</a></span>
            <span class="menuButton"><g:link class="list" action="list"><g:message code="default.list.label" args="[entityName]" /></g:link></span>
            <span class="menuButton"><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></span>
        </div>
        <div class="body">
            <h1><g:message code="default.edit.label" args="[entityName]" /></h1>
            <g:if test="\${flash.message}">
            <div class="message">\${flash.message}</div>
            </g:if>
            <g:hasErrors bean="\${${propertyName}}">
            <div class="errors">
                <g:renderErrors bean="\${${propertyName}}" as="list" />
            </div>
            </g:hasErrors>
            <g:form method="post" <%= multiPart ? ' enctype="multipart/form-data"' : '' %>>
                <g:hiddenField name="id" value="\${${propertyName}?.id}" />
                <g:hiddenField name="version" value="\${${propertyName}?.version}" />
                <div class="dialog">
                    <table>
                        <tbody>
                        <%  excludedProps = Event.allEvents.toList() << 'version' << 'id'
                            props = domainClass.properties.findAll { !excludedProps.contains(it.name) }
                            Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
                            props.each { p ->
                                if(!inlines || !inlines.find{item -> item.equals(p.name)}) {
                                  cp = domainClass.constrainedProperties[p.name]
                                  display = (cp ? cp.display : true)
                                  if (display) { %>
                              <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="${p.name}"><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></label>
                                </td>
                                <td valign="top" class="value \${hasErrors(bean: ${propertyName}, field: '${p.name}', 'errors')}">
                                    ${renderEditor(p)}
                                </td>
                            </tr>
                        <%  } }  } %>
                        </tbody>
                    </table>
                </div>
                <div class="buttons">
                    <span class="button"><g:actionSubmit class="save" action="update" value="\${message(code: 'default.button.update.label', default: 'Update')}" /></span>
                </div>
            </g:form>
        </div>
        <% if (inlines){  props.each { p -> %>
        <%if(inlines && inlines.find{item -> item.equals(p.name)}){ %>
        <script type="text/javascript">
        \$(function() {

          \$('#dialog-form-${p.name}').dialog({
            autoOpen: false,
            height: <%=(p.referencedDomainClass.properties.size() * 30)+60 %>,
            width: 350,
            modal: true,
            buttons: {
                'Create': function() {
                var posturl = '\${createLink(action: 'saveRalated')}';  
                \$.post(posturl,\$('#add_${p.name}').serialize() , function(data) {
                    \$('#${p.name}_list').append(data);
                  }, 'html');
                \$(this).dialog('close');
                },
                Cancel: function() {
                    \$(this).dialog('close');
                }    
            }
    });
    \$('#add_reminder_${p.name}').click(function() {
        clear_form_elements( \$('#dialog-form-${p.name}'));   
        \$('#dialog-form-${p.name}').dialog('open');
    });
});
        </script>
        <div id="dialog-form-${p.name}">
          <g:form action="save${p.name}" name="add_${p.name}" method="post">
           <table>
                        <tbody>
                        <%  excludedProps = Event.allEvents.toList() << 'version' << 'id'
                            def propsrel = p.referencedDomainClass.properties.findAll { !excludedProps.contains(it.name) }
                            Collections.sort(propsrel, comparator.constructors[0].newInstance([p.referencedDomainClass] as Object[]))
                            propsrel.each { prel ->

                                  cp = p.referencedDomainClass.constrainedProperties[p.name]
                                  display = (cp ? cp.display : true)
                                  if (display) { %>
                              <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="${prel.name}"><g:message code="${p.referencedDomainClass.propertyName}.${prel.name}.label" default="${prel.naturalName}" /></label>
                                </td>
                                <td valign="top" class="value \${hasErrors(bean: ${propertyName}, field: '${p.name}', 'errors')}">
                                    ${renderEditor(prel)}
                                </td>
                            </tr>
                        <%  }   } %>
                        <input type="hidden" name="idInlineOwner" value="\${${propertyName}.id}" />
                        <input type="hidden" name="ownedName" value="${p.name}" />
                        </tbody>
                    </table>
            </g:form>

        </div>


        <div class="body">
            <table id="${p.name}_list">


                                    <tr>
                                      <td>
                                          <h2>  <g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" />  </h2>
                                      </td>
                                      <td>
                                        <a href="#" id='add_reminder_${p.name}'>ADD</a>
                                      </td>
                                    </tr>
                                      <g:each in="\${${propertyName}.${p.name}}" var="${p.name[0]}" status="index">
                                       <tr id="${p.name}_\${${p.name[0]}?.id}_tr">
                                         <td>\${${p.name[0]}?.encodeAsHTML()}</td>
                                         <td>
                                          
                                            <g:remoteLink action="remove${p.name}" params="[id:${propertyName}.id , index:index]"
                                                    onSuccess="removeElement('${p.name}_\${${p.name[0]}?.id}_tr')">
                                              REMOVE
                                            </g:remoteLink>
                                         </td>
                                       </tr>
                                      </g:each>


                                  </table>
        </div>
       <% } } }%>

    </body>
</html>
